import { Component, OnInit } from '@angular/core';
import * as constants from '../../../providers/Constants'
import { ToastService } from './../../../providers/toast/toast.service'
import { Storage } from 'src/app/providers/Storage';
import { Utils } from 'src/app/providers/Utils';
import { LoanService } from 'src/app/services/loan.service';
import { AuthService } from 'src/app/services/auth.service';
import { NavController } from '@ionic/angular';
@Component({
  selector: 'app-updata-info',
  templateUrl: './updata-info.page.html',
  styleUrls: ['./updata-info.page.scss'],
})
export class UpdataInfoPage implements OnInit {
  userPhone: string = '';

  userNickName: string = '';
  constructor(private nav: NavController,
    private service: LoanService,
    private auth: AuthService,
    private toast: ToastService
  ) { }

  ngOnInit() {
    this.userPhone = Storage.localStorage.get('userPhone')
  }

  onClick() {
    if (!this.userNickName) {
      this.toast.show('please input ');
    } else {
      this.upData();
    }
  }
  upData() {
    this.service.modifyUserInfo({
      userPhone: this.userPhone,
      userNickName: this.userNickName
    }).subscribe(res => {
      // 保存成功返沪上一页，否则弹出提示框
      if (res.retCode == '000000') {
        // this.getUserInfo();
        let userInfo = Storage.localStorage.get('userInfo');
        userInfo.userNickName = this.userNickName;
        Storage.localStorage.set('userInfo', userInfo);
        this.nav.pop();
      } else if (constants.errCode[res.retCode]) {
        if (constants.errCode[res.retCode] != '0') {
          this.toast.show(constants.errCode[res.retCode]);
        }
      } else if (res.retMsg) {
        this.toast.show(res.retMsg);
      } else {
        this.toast.show("exception service routine");
      }
    });
  }
}
